package com.api.syh56.shengyunhui.controller.admin;

import cn.hutool.jwt.JWT;
import com.api.syh56.shengyunhui.common.BaseUserInfo;
import com.api.syh56.shengyunhui.common.CommonPage;
import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.common.UserInfoEnu;
import com.api.syh56.shengyunhui.entity.adminEntity.AdminUser;
import com.api.syh56.shengyunhui.entity.adminEntity.Permission;
import com.api.syh56.shengyunhui.entity.adminEntity.StaffMember;
import com.api.syh56.shengyunhui.exception.BaseException;
import com.api.syh56.shengyunhui.service.AdminSupService;
import com.api.syh56.shengyunhui.service.StaffMemberService;
import com.api.syh56.shengyunhui.util.JWTUtils;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 管理员管理
 */
@RestController
@RequestMapping("admin/user")
public class AdminSupController {

    @Autowired
    private AdminSupService adminSupService;

    @Autowired
    private StaffMemberService staffMemberService;

    /**
     * 添加工号
     * @param adminUser 管理员用户
     * @return
     */
    @PostMapping("jobNum")
    public CommonResult<Object> reg(@RequestBody AdminUser adminUser){
        String username = adminUser.getUsername();
        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AdminUser::getUsername,username);
        AdminUser user = adminSupService.getOne(wrapper);
        if (user != null) {
            throw new BaseException("该用户名已存在");
        }
        adminSupService.reg(adminUser);
        return CommonResult.success(null);
    }

    /**
     * 修改工号信息
     * @param adminUser
     * @return
     */
    @PostMapping("change")
    public CommonResult<Object> change(@RequestBody AdminUser adminUser){
        String password = adminUser.getPassword();
        if (password != null) {
            try{
                String md5 = "";
                md5 = DigestUtils.md5DigestAsHex(adminUser.getPassword().getBytes("utf-8"));
                adminUser.setPassword(md5);
            }catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }
        }
        adminSupService.updateById(adminUser);
        return CommonResult.success(null);
    }

    /**
     * 添加职员
     */
    @PostMapping("staffMember")
    private CommonResult<Object> addStaff(@RequestBody StaffMember staffMember){
        staffMemberService.save(staffMember);
        return CommonResult.success(null);
    }

    /**
     * 职员绑定工号
     */
//    @PostMapping("bindNum")
//    private CommonResult<Object> bind(@RequestBody Long aid, @RequestBody Long sid){
//
//    }

    /**
     * 登录
     * @param adminUser 管理员用户
     * @return
     */
    @PostMapping("login")
    public CommonResult<Object> login(@RequestBody AdminUser adminUser){
        HashMap<String,Object> login = adminSupService.login(adminUser);
        return CommonResult.success(login);
    }

    /**
     * 查询管理员列表 用户名/姓名/手机号/身份
     * @param pageNum
     * @param pageSize
     * @param userName 用户名
     * @param realName 真实姓名
     * @param phone 手机号
     * @param role 身份
     * @return
     */
    @GetMapping("get")
    public CommonResult<CommonPage<AdminUser>> get(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                   @RequestParam String userName,
                                                   @RequestParam String realName,
                                                   @RequestParam String phone,
                                                   @RequestParam String role){
        Page<AdminUser> adminUserPage = new Page<>(pageNum, pageSize);
        QueryWrapper<AdminUser> wrapper = new QueryWrapper<>();
        if (!userName.isEmpty()) {
            wrapper.lambda().eq(AdminUser::getUsername,userName);
        }
        if (!realName.isEmpty()) {
            wrapper.lambda().like(AdminUser::getRealName,realName);
        }
        if (!phone.isEmpty()) {
            wrapper.lambda().eq(AdminUser::getPhone,phone);
        }
        if (!role.isEmpty()){
            wrapper.lambda().eq(AdminUser::getRole,Short.valueOf(role));
        }
        Page<AdminUser> adminUsers = adminSupService.page(adminUserPage,wrapper);
        return CommonResult.success(CommonPage.restPage(adminUsers));
    }

    /**
     * 通过token获取用户详细信息
     * @param
     * @return
     */
    @GetMapping("getByToken")
//    public CommonResult<Object> getAUser(@RequestParam String token){
    public CommonResult<Object> getAUser(){
        String string = BaseUserInfo.get(UserInfoEnu.id.getCode());
//        DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
//        String string = tokenInfo.getClaim("id").asString();
        Long adminId = Long.valueOf(string);
        AdminUser adminUser = adminSupService.getById(adminId);
        return CommonResult.success(adminUser);
    }

    /**
     * 删除工号
     * @param adminId 工号id
     * @return
     */
    @GetMapping("remove")
    public CommonResult<Object> remove(@RequestParam String adminId){
        Long aLong = Long.valueOf(adminId);
        adminSupService.removeById(aLong);
        return CommonResult.success(null);
    }


    /**
     * 查询职员列表
     */
    @GetMapping("getStaffMember")
    public CommonResult<Object> getStaff(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){
        Page<StaffMember> staffMemberPage = new Page<>(pageNum, pageSize);
        Page<StaffMember> page = staffMemberService.page(staffMemberPage);
        return CommonResult.success(CommonPage.restPage(page));
    }



    /**
     * 添加管理权限
     * @param permission
     * @return
     */
    @PostMapping("/per/reg")
    public CommonResult<Object> regPer(@RequestBody Permission permission){
        adminSupService.regPer(permission);
        return CommonResult.success(null);
    }

    /**
     * 修改管理权限
     * @param permission 权限
     * @return
     */
    @PostMapping("/per/update")
    public CommonResult<Object> update(@RequestBody Permission permission){
        adminSupService.update(permission);
        return CommonResult.success(null);
    }

    /**
     * 获取管理权限
     * @return
     */
    @GetMapping("/per/get")
    public CommonResult<Object> getPer(){
        List<Permission> per = adminSupService.getPer();
        return CommonResult.success(per);
    }
}
